<?php include_once('header.php'); ?>

<?php include_once('left_navigation.php'); ?>

<?php
include('../classess/add_trainer.php');



$new_trainer = new add_trainer();



if (isset($_POST['user_name']) && trim($_POST['trainer_id']) == '') {

    $add_update_trainee = $new_trainer->new_trainer_add();
}



if (isset($_POST['trainer_id']) && trim($_POST['trainer_id']) != '') {

    $add_update_trainee = $new_trainer->update_trainer();
}



if (isset($_GET['id'])) {

    $get_trainee_row = $new_trainer->get_trainer($_GET['id']);
}



//del_id

if (isset($_GET['del_id'])) {

    $add_update_trainee = $new_trainer->del_trainer($_GET['del_id']);
}
?>

<div class="col-lg-12"><!-- Start Middle Content Div-->

    <div class="box">



        <div class="box-header" data-original-title=""><!-- Start Box Header Div-->

            <h2><i class="fa fa-edit"></i><span class="break"></span><?php if (isset($_GET['id'])) {
    echo 'Edit ';
} else {
    echo 'Add New ';
} ?>Trainer</h2>

            <div class="box-icon">

                <a href="gallery.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>

                <a href="gallery.html#" class="btn-close"><i class="fa fa-times"></i></a>

            </div>

        </div><!-- End Box Header Div-->

        <div class="box-content"><!-- Start Box Content Div-->

            <div class="row">



                <div class="col-sm-12">

<?php
if (isset($add_update_trainee)) {
    ?>

                        <div class="alert alert-info">

                            <div style="float: right; width: 5%;"><button type="button" class="close" data-dismiss="alert">&times;</button></div>

                            <div style="float:left;width: 92%;word-wrap: break-word;"><?php echo $add_update_trainee; ?></div>

                            <div style="clear:both;"></div>							

                        </div>

    <?php
}
?>

                    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?trner=1<?php if (isset($_GET['id'])) {
                        echo '&id=' . $_GET['id'];
                    } ?>" name="add_trainer_form" id="add_trainer_form" enctype="multipart/form-data">

                        <div class="main_form_box col-sm-6">

                            <div class="form-content  form-content-form">								

                                <label>Name</label>

                                <input type="text" placeholder="Name" name="user_name" id="user_name" value="<?php if (isset($get_trainee_row['0']->Name)) {
                        echo $get_trainee_row['0']->Name;
                    } ?>"> 



                                <label>Email</label>

                                <input type="text" placeholder="Email" name="user_email" id="user_email" value="<?php if (isset($get_trainee_row['0']->email)) {
                        echo $get_trainee_row['0']->email;
                    } ?>"> 



                                <?php
                               // if (!isset($_GET['id'])) {
                                    ?>

                                    <label>Password</label>

                                    <input type="password" name="user_pwd" id="user_pwd" placeholder="Password"> 

<?php //} ?>



                                <label>Skype Email</label>

                                <input type="text" placeholder="Skype Email" name="user_skype_email" id="user_skype_email" value="<?php if (isset($get_trainee_row['0']->skype_mail)) {
    echo $get_trainee_row['0']->skype_mail;
} ?>"> 



                                <label>Contact Number</label>

                                <input type="text" placeholder="Contact Number" name="user_con_num" id="user_con_num" value="<?php if (isset($get_trainee_row['0']->contact_number)) {
    echo $get_trainee_row['0']->contact_number;
} ?>"> 



                                <label>Profile Image</label>

                                <input type="file" name="user_prof" id="user_prof"> 

                                <label>Commision (%)</label>

                                <input type="text" name="commision" id="commision" value="<?php if (isset($get_trainee_row['0']->commision)) {
    echo $get_trainee_row['0']->commision;} ?>"> 
                                
                            </div>

                        </div>



                        <div class="main_form_box col-sm-6" >

                            <div class="form-content  form-content-form">

                                <label>Address</label>

                                <textarea name="user_address" id="user_address"> 

<?php if (isset($get_trainee_row['0']->Address)) {
    echo $get_trainee_row['0']->Address;
} ?>

                                </textarea> 



                                <label>Course</label>

                                <select id="user_course" name="user_course">

                                    <?php
                                    $select = "SELECT * FROM tbl_courses";

                                    $query = mysql_query($select) or die(mysql_error());

                                    if (mysql_num_rows($query) > 0) {

                                        while ($row = mysql_fetch_array($query)) {
                                            ?> 

                                            <option value="<?php echo $row['id']; ?>" <?php if (isset($get_trainee_row['0']->course)) {
                                        if ($get_trainee_row['0']->course == $row['id']) {
                                            echo 'selected="selected"';
                                        }
                                    } ?> ><?php echo $row['course_name']; ?></option>

                                            <?php
                                        }
                                    } else {

                                        echo '<option value="">No Courses</option>';
                                    }
                                    ?>

                                </select>

                                                                <!--<input type="text" name="user_course" id="user_course" value="<?php if (isset($get_trainee_row['0']->course)) {
                                        echo $get_trainee_row['0']->course;
                                    } ?>"> -->


                                

                                <label>Is Admin</label>

                                <select id="is_admin" name="is_admin">

                                    <option value="1" <?php if (isset($get_trainee_row['0']->is_admin)) {
                                        if ($get_trainee_row['0']->is_admin == 1) echo 'selected="selected"';
                                    } ?>>Yes</option>

                                    <option value="0" <?php if (isset($get_trainee_row['0']->is_admin)) {
                                        if ($get_trainee_row['0']->is_admin == 0) echo 'selected="selected"';
                                    } ?>>No</option>

                                </select>



                                <label>Is Trainer</label>

                                <select id="is_trainer" name="is_trainer">

                                    <option value="1" <?php if (isset($get_trainee_row['0']->is_trainer)) {
                                        if ($get_trainee_row['0']->is_trainer == 1) echo 'selected="selected"';
                                    } ?>>Yes</option>

                                    <option value="0" <?php if (isset($get_trainee_row['0']->is_trainer)) {
                                        if ($get_trainee_row['0']->is_trainer == 0) echo 'selected="selected"';
                                    } ?>>No</option>

                                </select>



                                <label>Is Active</label>

                                <select id="is_active" name="is_active">

                                    <option value="1" <?php if (isset($get_trainee_row['0']->is_active)) {
                                        if ($get_trainee_row['0']->is_active == 1) echo 'selected="selected"';
                                    } ?>>Yes</option>

                                    <option value="0" <?php if (isset($get_trainee_row['0']->is_active)) {
                                        if ($get_trainee_row['0']->is_active == 0) echo 'selected="selected"';
                                    } ?>>No</option>

                                </select>



                                <label style="text-align:right;">

                                    <button type="button" class="btn btn-blue login_button" name="add_user" id="add_user"><?php if (isset($_GET['id'])) {
                                        echo 'Edit';
                                    } else {
                                        echo 'Add';
                                    } ?></button> 							

                                </label>								

                            </div>

                        </div>

                        <div style="clear:both;"></div>

                        <input type="hidden" name="trainer_id" id="trainer_id" value="<?php if (isset($_GET['id'])) {
                                        echo $_GET['id'];
                                    } ?>">

                    </form>



                </div><!--/col-->

            </div><!-- End Row div -->

        </div><!-- End Box Content Div -->



    </div><!-- End Box Div-->

</div><!-- End Middle Content Div-->

<div class="col-lg-12"><!-- Start Middle Content Div-->

    <div class="box">

        <div class="row">		

            <div class="col-lg-12">

                <div class="box">

                    <div class="box-header" data-original-title>

                        <h2><i class="fa fa-user"></i><span class="break"></span>Members</h2>

                        <div class="box-icon">

                            <a href="table.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>

                            <a href="table.html#" class="btn-close"><i class="fa fa-times"></i></a>

                        </div>

                    </div>

                    <div class="box-content">

                        <div class="search_box">

                            <!--<div class="fl_left sel_label_search">Search:</div>

                            <div class="fl_left" style="width:60%;">									

                                    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?trner=1" id="search_form" name="search_form">

                                            <input type="text" name="search_value" id="search_value" placeholder="Search" value="<?php if (isset($_POST['search_value'])) {
                                        echo $_POST['search_value'];
                                    } else {
                                        echo '';
                                    } if (isset($_GET['srch_value'])) {
                                        echo $_GET['srch_value'];
                                    } else {
                                        echo '';
                                    } ?>">&nbsp;

                                            <input type="submit" name="submit_search" id="submit_search" class="btn btn-blue srch_btn" value="Search">

                                    </form>



                            </div>

                            <div class="clear_fix"></div>-->

                            <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?trner=1" id="search_form" name="search_form">

                                <div class="fl_left" style="width:50%;">

                                    <div class="fl_left sel_label_search" style="width:11%;">Search:</div>

                                    <div class="fl_left" style="width:86%;">	

                                        <input type="text" name="search_value" id="search_value" placeholder="Search" value="<?php if (isset($_POST['search_value'])) {
                                            echo $_POST['search_value'];
                                        } else {
                                            echo '';
                                        } if (isset($_GET['srch_value'])) {
                                            echo $_GET['srch_value'];
                                        } else {
                                            echo '';
                                        } ?>" style="width:45%;">&nbsp;

                                        <input type="submit" name="submit_search" id="submit_search" class="btn btn-blue srch_btn" value="Search">										

                                    </div>

                                    <div class="clear_fix"></div>

                                </div>

                                <div class="fl_right">

                                    <div class="fl_left" style="width:15%; margin-top:5px;">Number Of Records : </div>

                                    <div class="fl_left">

                                        <?php
                                        $limit_10 = '';

                                        $limit_25 = '';

                                        $limit_50 = '';

                                        $limit_100 = '';

                                        if (isset($_POST['sel_limit'])) {

                                            if ($_POST['sel_limit'] == '10') {

                                                $limit_10 = "selected='selected'";
                                            }
                                            

                                            if ($_POST['sel_limit'] == '25') {

                                                $limit_25 = "selected='selected'";
                                            }

                                            if ($_POST['sel_limit'] == '50') {

                                                $limit_50 = "selected='selected'";
                                            }

                                            if ($_POST['sel_limit'] == '100') {

                                                $limit_100 = "selected='selected'";
                                            }
                                        }

                                        if (isset($_GET['sel_get_limit'])) {

                                            if ($_GET['sel_get_limit'] == '10') {

                                                $limit_10 = "selected='selected'";
                                            }

                                            if ($_GET['sel_get_limit'] == '25') {

                                                $limit_25 = "selected='selected'";
                                            }

                                            if ($_GET['sel_get_limit'] == '50') {

                                                $limit_50 = "selected='selected'";
                                            }

                                            if ($_GET['sel_get_limit'] == '100') {

                                                $limit_100 = "selected='selected'";
                                            }
                                        }
                                        ?>

                                        <select name="sel_limit" id="sel_limit" onchange="javascript:document.search_form.submit();">

                                          <!--  <option value="10" <?php echo $limit_10; ?> >10</option>
-->
                                            <option value="25" <?php echo $limit_25; ?> >25</option>

                                            <option value="50" <?php echo $limit_50; ?> >50</option>

                                            <option value="100" <?php echo $limit_100; ?> >100</option>

                                        </select>



                                    </div>

                                    <div class="clear_fix"></div>

                                </div>

                                <div class="clear_fix"></div>							

                            </form>

                        </div>

                        <table class="table table-striped table-bordered">

                            <thead>

                                <tr>

                                    <th>Name</th>

                                    <th>Email</th>

                                    <th>Contact Number</th>

                                    <th>Course</th>

                                    <th>Is Admin</th>

                                    <th>Is Trainer</th>

                                    <th>Is Active</th>

                                    <th>Actions</th>

                                </tr>

                            </thead>   

                            <tbody>

                                <?php
                                $rec_limit = 25;

                                if (isset($_POST['sel_limit'])) {

                                    $rec_limit = $_POST['sel_limit'];
                                }



                                if (isset($_GET['sel_get_limit'])) {

                                    $rec_limit = $_GET['sel_get_limit'];
                                }



                                $condition_search = '';

                                $value = '';

                                if (isset($_POST['search_value'])) {

                                    $value = trim($_POST['search_value']);



                                    if (!empty($value)) {

                                        $cond_where = ' WHERE';

                                        $condition_search = $cond_where . " (Name like '%" . $value . "%' OR email like '%" . $value . "%' OR skype_mail like '%" . $value . "%' OR contact_number like '%" . $value . "%' OR Address like '%" . $value . "%')";
                                    }
                                }



                                if (isset($_GET['srch_value'])) {

                                    $value = $_GET['srch_value'];



                                    if (!empty($value)) {

                                        $cond_where = ' WHERE';



                                        $condition_search = $cond_where . " (Name like '%" . $value . "%' OR email like '%" . $value . "%' OR skype_mail like '%" . $value . "%' OR contact_number like '%" . $value . "%' OR Address like '%" . $value . "%')";
                                    }
                                }



                                $select_count_course = "SELECT count(id) FROM tbl_admin" . $condition_search;

                                $result_count_course = mysql_query($select_count_course);



                                $row = mysql_fetch_array($result_count_course);

                                $rec_count = $row[0];



                                if (isset($_GET['page'])) {

                                    $page = $_GET['page'] + 1;

                                    $offset = $rec_limit * $page;
                                } else {

                                    $page = 0;

                                    $offset = 0;
                                }



                                $serial_number = $page * $rec_limit;



                                $left_rec = $rec_count - ($page * $rec_limit);



                                $select_trainer = "select * from tbl_admin" . $condition_search . " ORDER BY id DESC LIMIT " . $offset . ", " . $rec_limit;

                                $query_trainer = mysql_query($select_trainer) or die(mysql_error());



                                $i = 1;

                                if (mysql_num_rows($query_trainer) > 0) {

                                    while ($row = mysql_fetch_array($query_trainer)) {

                                        $serial_number++;

                                        echo '

											<tr>

												<td>' . $row['Name'] . '</td>

												<td>' . $row['email'] . '</td>

												<td>' . $row['contact_number'] . '</td>

												<td>';

                                        $sel_course = "select * from tbl_courses where id='" . $row['course'] . "'";

                                        $course_query = mysql_query($sel_course) or die(mysql_error());

                                        if (mysql_num_rows($course_query) > 0) {

                                            while ($row_course = mysql_fetch_array($course_query)) {

                                                echo $row_course['course_name'];
                                            }
                                        } else {

                                            echo 'No COurse Availabale';
                                        }

                                        echo '</td>

												<td>';

                                        if ($row['is_admin']) {

                                            echo 'Yes';
                                        } else {

                                            echo 'No';
                                        }

                                        echo '</td>

												<td>';

                                        if ($row['is_trainer']) {

                                            echo 'Yes';
                                        } else {

                                            echo 'No';
                                        }

                                        echo '</td>

												<td>';

                                        if ($row['is_active']) {

                                            echo '<span class="label label-success">Active</span>';
                                        } else {

                                            echo '<span class="label label-default">Inactive</span>';
                                        }

                                        echo '</td>

												<td>													

													<a class="btn btn-info" href="' . $_SERVER['PHP_SELF'] . '?trner=1&id=' . $row['id'] . '" title="Edit">

														<i class="fa fa-edit "></i>  

													</a>

													<a class="btn btn-danger" href="' . $_SERVER['PHP_SELF'] . '?trner=1&del_id=' . $row['id'] . '" title="Delete">

														<i class="fa fa-trash-o "></i> 

													</a>

												</td>

											</tr>

										';

                                        $i++;
                                    }
                                } else {

                                    echo '<tr>

										<td colspan="9" style="text-align:center;">No Records Found</td>

									</tr>';
                                }
                                ?>						

                            </tbody>

                        </table>  

<?php
echo '<ul class="pagination">';

if ($rec_count > $rec_limit) {

    if ($page > 0 && $left_rec > $rec_limit) {

        $last = $page - 2;

        echo '<li><a href="' . $_SERVER["PHP_SELF"] . '?trner=1&page=' . $last . '&srch_value=' . $value . '&sel_get_limit=' . $rec_limit . '" class="anc_click">Prev</a></li> ';

        echo '<li><a href="' . $_SERVER["PHP_SELF"] . '?trner=1&page=' . $page . '&srch_value=' . $value . '&sel_get_limit=' . $rec_limit . '" class="anc_click">Next</a></li>';
    } else if ($page == 0) {

        echo '<li><a href="' . $_SERVER["PHP_SELF"] . '?trner=1&page=' . $page . '&srch_value=' . $value . '&sel_get_limit=' . $rec_limit . '" class="anc_click">Next</a></li>';
    } else if ($left_rec < $rec_limit || $left_rec == $rec_limit) {

        $last = $page - 2;

        echo '<li><a href="' . $_SERVER["PHP_SELF"] . '?trner=1&page=' . $last . '&srch_value=' . $value . '&sel_get_limit=' . $rec_limit . '" class="anc_click">Prev</a></li>';
    }
}

echo '</ul>';
?>

                    </div>

                </div>

            </div><!--/col-->



        </div><!--/row-->

    </div><!-- End Box Div-->

</div><!-- End Middle Content Div-->

                        <?php include_once('footer.php'); ?>

<script>

                                                                            $ = jQuery.noConflict();

                                                                            $(document).ready(function() {

                                                                                var regex = /(\d.*){10}/;

                                                                                var regex1 = /([^\d]+)/g;

                                                                                var phnVal = $('#user_con_num').val().replace(regex1, '');

                                                                                var number = /(\d)\1{9}/;

                                                                                $('#add_user').click(function() {

                                                                                    if ($('#user_name').val() == '') {

                                                                                        alert("Please enter a value for the \" Name\" field.");

                                                                                        $('#user_name').focus();

                                                                                        return (false);

                                                                                    }

                                                                                    else if ($('#user_email').val() == "")

                                                                                    {

                                                                                        alert("Please enter a value for the \"Email\" field.");

                                                                                        $('#user_email').focus();

                                                                                        return (false);

                                                                                    }

                                                                                    else if (($('#user_email').val().indexOf("\@") == -1) || ($('#user_email').val().indexOf(".") == -1) || isInvalidateEmail($('#user_email').val()))

                                                                                    {

                                                                                        alert("Invalid Email Address");

                                                                                        $('#user_email').focus();

                                                                                        return (false);

                                                                                    }

                                                                                    if ($('#user_pwd').val() == '') {

                                                                                        alert("Please enter a value for the \" Password\" field.");

                                                                                        $('#user_pwd').focus();

                                                                                        return (false);

                                                                                    }

                                                                                    else if ($('#user_con_num').val() == "") {

                                                                                        alert("Please enter a value for the \" Contact Number \" field.");

                                                                                        $('#user_con_num').focus();

                                                                                        return (false);

                                                                                    }

                                                                                    else if (!$('#user_con_num').val().match(regex))

                                                                                    {

                                                                                        alert("Please enter your complete 10 digit phone number.");

                                                                                        $('#user_con_num').focus();

                                                                                        return (false);

                                                                                    }

                                                                                    else if (phnVal.match(number))

                                                                                    {

                                                                                        alert("Please enter proper 10 digit phone number.");

                                                                                        $('#user_con_num').focus();

                                                                                        return (false);

                                                                                    }



                                                                                    else if ($('#user_address').val() == "") {

                                                                                        alert("Please enter a value for the \" Address \" field.");

                                                                                        $('#user_address').focus();

                                                                                        return (false);

                                                                                    }

                                                                                    else if ($('#user_course').val() == "") {

                                                                                        alert("Please enter a value for the \"User Course \" field.");

                                                                                        $('#user_course').focus();

                                                                                        return (false);

                                                                                    }

                                                                                    else {

                                                                                        document.add_trainer_form.submit();

                                                                                    }

                                                                                });



                                                                                $('.close').click(function() {

                                                                                    $('.alert-info').addClass('hide');

                                                                                });

                                                                            });



                                                                            function isInvalidateEmail(str)

                                                                            {

                                                                                var atpos = str.indexOf("@");

                                                                                var dotpos = str.lastIndexOf(".");

                                                                                if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= str.length)

                                                                                {

                                                                                    return true;

                                                                                }

                                                                                return false;

                                                                            }

</script>